package com.junjun.ssm.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.junjun.ssm.dao.OrderDao;
import com.junjun.ssm.dto.OrderDto;
import com.junjun.ssm.entity.OrderEntity;
import com.junjun.ssm.util.ConvertBeanUtil;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Configuration
public class ThreadConfig {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RabbitTemplate rabbitTemplate;



    /**
     * MQ投递后的回调函数
     */
    RabbitTemplate.ConfirmCallback callback=(CorrelationData correlationData, boolean bl, String s)->{
        //判断投递是否成功
        if(bl){
            /*System.out.println(correlationData.getId());
            //业务处理
            OrderEntity orderEntity = orderDao.findById(Long.parseLong(correlationData.getId())).get();
            orderEntity.setTag(1);
            orderDao.save(orderEntity);*/
           /* OrderEntity orderEntity = JSON.parseObject(correlationData.getId(), OrderEntity.class);
            orderEntity.setState(1);
            orderDao.save(orderEntity);*/
            System.out.println("投递MQ成功");
        }
    };

    @Bean
    public ExecutorService getExcutorService() throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    while(true) {

                        String order = (String) redisTemplate.opsForList().rightPop("order",5,TimeUnit.SECONDS);
                        if (order != null && order != ""){

                            OrderEntity orderEntity1 = JSON.parseObject(order, OrderEntity.class);
                            OrderDto orderDto = ConvertBeanUtil.copy(orderEntity1, OrderDto.class);
                            String json = JSON.toJSONString(orderEntity1);
                            String orderId = String.valueOf(json);

                            rabbitTemplate.setMandatory(true);//开启MQ消息确认
                            rabbitTemplate.setConfirmCallback(callback);//绑定MQ回调函数
                            CorrelationData c = new CorrelationData(orderId);//创建MQ关联数据对象
                            rabbitTemplate.convertAndSend("directExchange", "movie.order", orderDto, c);
                            //System.out.println(Thread.currentThread().getName());
                        }
                       // System.out.println("sdfsf");
                    }
                }
            });
            Thread.sleep(200);
        }
        return executorService;

    }
}
